package com.test.personalblogproject.service.impl;

import com.test.personalblogproject.mapper.ArticleBodyMapper;
import com.test.personalblogproject.mapper.ArticleMapper;
import com.test.personalblogproject.mapper.ArticleTagMapper;
import com.test.personalblogproject.mapper.UserMapper;
import com.test.personalblogproject.pojo.*;
import com.test.personalblogproject.service.ArticleService;
import com.test.personalblogproject.service.UserService;
import com.test.personalblogproject.utils.Md5Util;
import com.test.personalblogproject.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleTagMapper articleTagMapper;
    @Autowired
    private ArticleBodyMapper articleBodyMapper;

    @Override
    @Transactional
    public Result publish(ArticleParam articleParam) {
        //获取当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User sysUser = userMapper.findByUserName(username);
        //创建文章
        Article article = new Article();
        article.setAuthorId(Long.valueOf(sysUser.getId()));
        article.setCategoryId(Long.valueOf(articleParam.getCategory().getId()));
        article.setCreateDate(System.currentTimeMillis());
        article.setCommentCounts(0);
        article.setSummary(articleParam.getSummary());
        article.setTitle(articleParam.getTitle());
        //默认0
        article.setViewCounts(0);
        //默认普通文章
        article.setWeight(Article.Article_Common);
        //默认没有bodyId
        article.setBodyId(-1L);
        //插入数据库
        this.articleMapper.insert(article);

        //tags
        List<TagVo> tags = articleParam.getTags();
        if (tags != null) {
            for (TagVo tag : tags) {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setArticleId(article.getId());
                articleTag.setTagId(tag.getId());
                this.articleTagMapper.insert(articleTag);
            }
        }
        //body
        ArticleBody articleBody = new ArticleBody();
        articleBody.setContent(articleParam.getBody().getContent());
        articleBody.setContentHtml(articleParam.getBody().getContentHtml());
        articleBody.setArticleId(article.getId());
        articleBodyMapper.insert(articleBody);
        //更新article
        article.setBodyId(articleBody.getId());
        //articleMapper.updateById(article);
        //返回文章id
        Article articleVo = new Article();
        articleVo.setId(article.getId());
        //return Result.success("id:"+articleVo.getId());
        Map<String, Object> data = Map.of("id", article.getId());
        return new Result<>(200, "success", data);   // 新
    }


}
